package com.frogs.ucenter.service.daoimpl;

import com.frogs.framework.base.dao.BaseDao;
import com.frogs.framework.base.service.daoimpl.BaseServiceImpl;
import com.frogs.framework.exception.BusinessRuntimeException;
import com.frogs.framework.util.PageList;
import com.frogs.ucenter.dao.UserMessageSmsLogDao;
import com.frogs.ucenter.entity.UserMessageSmsLog;
import com.frogs.ucenter.service.UserMessageSmsLogService;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service("userMessageSmsLogService")
public class UserMessageSmsLogServiceImpl extends BaseServiceImpl<UserMessageSmsLog> implements UserMessageSmsLogService {
    @Autowired
    private UserMessageSmsLogDao userMessageSmsLogDao;

    public UserMessageSmsLogServiceImpl() {
        super();
    }

    public UserMessageSmsLogDao getUserMessageSmsLogDao() {
        return userMessageSmsLogDao;
    }

    /**
     * Don't remove or edit it.
     */
    @Override
    protected BaseDao<UserMessageSmsLog> getBaseDao() {
        return userMessageSmsLogDao;
    }

    public void setUserMessageSmsLogDao(UserMessageSmsLogDao userMessageSmsLogDao) {
        this.userMessageSmsLogDao = userMessageSmsLogDao;
    }

    public PageList<UserMessageSmsLog> getAllParentList(Integer id) {
        Map<String,Object> params = new HashMap<String,Object>(); 
        params.put( "eqIdAsChildId", id);
        return getList( params, 1, 1000, false );
    }

    public PageList<UserMessageSmsLog> getAllChildList(Integer id, int pageNum, int numPerPage) {
        Map<String,Object> params = new HashMap<String,Object>(); 
        params.put( "eqIdAsParentId", id);
        return getList( params, pageNum, numPerPage, true );
    }

    public PageList<UserMessageSmsLog> getAllTopLevelList() {
        Map<String,Object> params = new HashMap<String,Object>(); 
        params.put( "eqParentId", 0 );
        return getList( params, 1, 1000, false );
    }

    @Override
    public int delete(Object id) {
        Map<String,Object> params = new HashMap<String,Object>(); 
        params.put( "eqParentId", id );
        int count = userMessageSmsLogDao.selectCountByMap( params );
        if( count > 0 ){
            throw new BusinessRuntimeException( "error.userMessageSmsLog.has.childUserMessageSmsLog" );
        }
        return userMessageSmsLogDao.deleteById( id );
    }

    @Override
    public int delete(Map<String, Object> param) {
        Map<String,Object> countparams = new HashMap<String,Object>(); 
        if( param.containsKey( "eqUserMessageSmsLogId" ) ){
            countparams.put( "eqParentId", param.get( "eqUserMessageSmsLogId" ) );
        }
        if( param.containsKey( "inUserMessageSmsLogIds" ) ){
            countparams.put( "inParentIds", param.get( "inUserMessageSmsLogIds" ) );
        }
        if( param.containsKey( "inUserMessageSmsLogIdList" ) ){
            countparams.put( "inParentIdList", param.get( "inUserMessageSmsLogIdList" ) );
        }
        if( countparams.size() > 0 ){
            int count = userMessageSmsLogDao.selectCountByMap( countparams );
            if( count > 0 ){
                throw new BusinessRuntimeException( "error.userMessageSmsLog.has.childUserMessageSmsLog" );
            }
        }
        return userMessageSmsLogDao.deleteByMap( param );
    }
}